<?php

namespace app\api\command;
use app\common\model\Packages;
use app\common\model\UserCommission;
use app\common\model\UserPackagesConfig;
use fast\Random;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class OrderSettlement extends Command
{
    protected function configure()
    {
        $this->setName('order_settlement')->setHelp('待结算订单结算')->setDescription('待结算订单结算');

    }

    protected function execute(Input $input, Output $output)
    {
        $orderModel = new \app\common\model\Order();
        //订单状态:0=已取消,1=待充值,2=待收货,3=待处理,4=待结算,5=已结算,6=话费报销
        $order      = $orderModel->where(['platform_type'=>4])->where(['is_agency'=>1])->whereIn('order_status', '1,2,3,4')->order('id desc')->select();
//        $order      = $orderModel->where(['id'=>295])->order('id desc')->select();
        if ($order) {
            foreach ($order as $k => $v) {
                sleep(2);
                $packageInfo = Packages::where(['id' => $v['goods_id']])->find();
                $this->orderInfo172($v, $packageInfo);
            }
        }
    }

    public function orderInfo172($orderInfo, $packageInfo)
    {
        $url                 = config('site.url_172');
        $key                 = config('site.key_172');
        $user_id             = config('site.zhanghao_172');
        $data['user_id']     = $user_id;
        $data['Timestamp']   = time();
        $data['DownOrderID'] = $orderInfo['order_id'];
        $string              = ascll172($data);
        $data['user_sign']   = strtolower(md5($string.$key));
        $url                 = $url.'/api/order/GetOrderInfo';
        $res                 = httpPost($url, $data);
        $arr                 = json_decode($res, true);
        if (!empty($arr['data']['orderNo'])) {
            if(!empty($arr['data']['expressName'])){
                $orderInfo->shipping_company_name = $arr['data']['expressName'];
            }
            if(!empty($arr['data']['expressCode'])){
                $orderInfo->shipping_no           = $arr['data']['expressCode'];
            }
            if(!empty($arr['data']['thirdPhone'])){
                $orderInfo->phone           = $arr['data']['thirdPhone'];
            }
            if (!empty($arr['data']['remark'])) {
                $orderInfo->remark          = $arr['data']['remark'];
            }
            if ($arr['data']['orderStatus'] == '已结算') {
                $orderInfo->remark          .= '+'.$arr['data']['orderStatus'];
                $orderInfo->is_hexiao       = 2;
                $orderInfo->order_status    = 5;
                $orderInfo->settlement_time = time();
//                $userData                   = findAncestors($orderInfo['user_id']);
//                if (!$userData) {
//                    //顶级代理下单
//                    dingji($orderInfo['user_id'], $orderInfo['id'], $orderInfo['goods_id'], $packageInfo['rebate'],
//                        false, true);
//                } else {
//                    array_unshift($userData, $orderInfo['user_id']);
//                    $money = moneydigui($orderInfo['user_id'], $orderInfo['goods_id']);
//                    if ($money) {
//                        digui($orderInfo['user_id'], $money, $orderInfo['goods_id'], $orderInfo['id'], 0, true);
//                    } else {
//                        //顶级
//                        dingji($userData[count($userData) - 1], $orderInfo['id'], $orderInfo['goods_id'],
//                            $packageInfo['rebate'], false, true);
//                    }
//                }
                orderfencheng($orderInfo['id'],$orderInfo['goods_id']);
            }elseif($arr['data']['orderStatus'] =='已发货'){
                $orderInfo->order_status = 2;
            }elseif($arr['data']['orderStatus'] =='支付完成'){
                $orderInfo->order_status = 4;
            }elseif($arr['data']['orderStatus'] =='待处理' || $arr['data']['orderStatus'] =='已提运营商'){
                $orderInfo->order_status = 3;
            }elseif($arr['data']['orderStatus'] == "审核不通过" || $arr['data']['orderStatus'] == "已撤单" || $arr['data']['orderStatus'] == "已取消" || $arr['data']['orderStatus'] == "无法结算"){
                $orderInfo->order_status = 0;
            }
            $orderInfo->order_info_json = json_encode($arr['data'],true);
            $orderInfo->save();
            echo $arr['data']['orderStatus'];
        }
    }
}
